package com.lsz.flight_system.service.Impl;

import com.lsz.flight_system.entity.UserAssess;
import com.lsz.flight_system.repository.UserAssessRepository;
import com.lsz.flight_system.service.UserAssessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserAssessServiceImpl implements UserAssessService {

    @Autowired
    UserAssessRepository userAssessRepository;

    @Override
    public List<UserAssess> adminFindAllUserAssess(Map<String, Object> map) {
        return userAssessRepository.findAll(new Specification<UserAssess>() {
            @Override
            public Predicate toPredicate(Root<UserAssess> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
//                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

                if (map.containsKey("username") && !StringUtils.isEmpty(map.get("username"))){
                    predicateList
                            .add(cb.like(root.get("username"),"%"+map.get("username").toString()+"%"));
                }
                if (map.containsKey("score") && !StringUtils.isEmpty(map.get("score"))){
                    predicateList
                            .add(cb.equal(root.get("score").as(String.class),map.get("score")));
                }
//                // 评价时间
//                if (map.containsKey("assessTime") && !StringUtils.isEmpty(map.get("assessTime"))) {
//                    LocalDateTime startTime = LocalDateTime.parse(map.get("assessTime").toString()+" 00:00:00", fmt);
//                    LocalDateTime endTime = LocalDateTime.parse(map.get("assessTime").toString()+ " 23:59:59", fmt);
//                    Predicate predicate = cb.between(root.get("assessTime").as(LocalDateTime.class),startTime,endTime);
//                    predicateList.add(predicate);
//                }

                Predicate[] predicateArr = new Predicate[predicateList.size()];
                return cb.and(predicateList.toArray(predicateArr));
            }
        });
    }

    //用户看到的意见反馈，选着匿名的不展示用户名
    @Override
    public List<UserAssess> userFindAllUserAssess() {
        List<UserAssess> assessList = userAssessRepository.findAll();
        List<UserAssess> userAssessList = new ArrayList<>();
        for (UserAssess assess:assessList){
            if (assess.getUnnamed().equals("是")){
                assess.setUsername("匿名");
            }
            userAssessList.add(assess);
        }
        return userAssessList;
    }

    @Override
    public boolean delUserAssessById(Long id) {
        userAssessRepository.deleteById(id);
        UserAssess userAssess = userAssessRepository.findUserAssessById(id);
        if (userAssess==null){
            return true;
        }
        return false;
    }

    @Override
    public boolean userAddUserAssess(UserAssess userAssess) {
        userAssess.setAssessTime(LocalDateTime.now());
        try {
            userAssessRepository.save(userAssess);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public UserAssess findUserAssessById(Long id) {
        return userAssessRepository.findUserAssessById(id);
    }

}
